var web = web || {};
web.ShippingCart = Class.create(); 
web.ShippingCart.prototype = {
	initialize: function(options) {
		var _this = this;
		this.settings = $.extend({
			ids: {fixed:'my-cart-fixed', floating:'my-cart-floating'}
		}, options);
		
		this.isIE6 = ($.browser.msie && $.browser.version < 7);
		this.win = $(window);
		this.isClose = true;
		this.hasNotViewCss = false;
		this.updateTime = 0;
		this.fixedTop = 0;
		
		this.fixedCart = null;
		this.fixedBox = null;
		this.fixedPanel = null;
		this.floatPanel = null;
		this.panelItem = null;
		
		this.fixedCartOffset = null;
		this.template = {cart: null, goods: null};
		
		this.init();
	},
	init: function() {
		var _this = this;
		this.fixedCart = $('#'+ this.settings.ids.fixed);
		this.floatPanel = $('#'+ this.settings.ids.floating);
		if (1 != this.fixedCart.size() || 1 != this.floatPanel.size()) return;
		
		this.fixedBox = this.fixedCart.children('.cart-box');
		this.fixedPanel = this.fixedCart.children('.cart-panel');
		
		this._setTemplate();
		
		this.fixedBox.bind('mouseenter', function(){
			_this._showPanel();
		});
		
		if ($.browser.msie) {
			document.onfocusin = function(){
				_this._updateCartItemCount();
			}
		} else {
			window.onfocus = function(){
				_this._updateCartItemCount();
			}
		}
				
		this.win.bind({
			resize: function(){_this._windowResize();},
			scroll: function(){_this._windowScroll();}
		});
	},
	bindAddGoodsEvent: function(selector, buyHander) {
		var _this = this;
		$(selector).click(function(){
			var goods = buyHander.call($(this));
			if (goods.id && goods.qty) { 
				_this._addGoodsToCart(goods.id, goods.qty);
			}
		});		
	},
	updateCartItemCount: function() {
		this._updateCartItemCount();
	},
	_setTemplate: function() {
		var c = '<div class="cp-title clearfix">'+
						'	<div class="cpt-text"><i class="icon notext i-my-cart-panel inline-block"></i><span>My Shipping Cart</span></div>'+
						'	<div class="cpt-action"><a class="i-panel-close cpt-close" href="javascript:noLink();" hidefocus="true" rel="nofollow"><i class="icon notext inline-block"></i><span>Close</span></a></div>'+
						'</div>'+
						'<div class="cp-body">'+
						'	<div class="item-list">'+
						'		<ul>'+
						'  	</ul>'+
						'	</div>'+
						'	<div class="subtotal">'+
						'		<strong>Subtotal</strong><span>({$itemcount} items):</span><span class="price"><span us-dollar="{$totalmoney}" class="money">loading</span></span>'+
						'	</div>'+
						'	<p class="checkout"><img width="200" height="25" border="0" class="CheckoutBtn" alt="Checkout" src="GotoCartandCheckOut_200x25.gif"></p>'+
						'</div>';
							
		var g = '<li>'+
						'	<div class="item-show clearfix">'+
						'		<img src="{$album}">'+
						'		<div class="goods">'+
						'			<a href="{$url}">{$title}</a>'+
						'			<div class="buy clearfix">'+
						'				<span class="qty">Qty: {$qty}</span>'+
						'				<span class="price">Price:<span us-dollar="{$price}" class="money">loading</span></span>'+
						'			</div>'+
						'		</div>'+
						'	</div>'+
						'</li>';
		
		this.template.cart = c;
		this.template.goods = g;
	},
	_windowResize: function() {
		if (this.isClose) return;
		this._closePanel();
		this.fixedTop = 0;
	},
	_windowScroll: function() {
		if (this.isClose) return;
		this._showPanelByScrollTop();
	},
	_showPanel: function() {
		if (!this.isClose) return;
		
		if (0 == this.fixedTop) {
			this.fixedCartOffset = this.fixedCart.offset();
			this.fixedTop = this.fixedCartOffset.top + this.fixedCart.outerHeight(true);
		}
		
		this._showPanelByScrollTop();
		this._setFloatingPanelFixed();
		this.isClose = false;
		
		if (this._isNeedUpdateCart()) {
			this._updateShippingCart();
		}
	},
	_closePanel: function() {
		this.fixedPanel.hide();
		this.floatPanel.hide();
		this.isClose = true;
	},
	_showPanelByScrollTop: function() {
		var top = this.win.scrollTop();
		if (top > this.fixedTop) {
			this._showFloatingPanel();
		} else {
			this._showFixedPanel();
		}
	},
	_setFloatingPanelFixed: function() {
		var panelWidth = this.fixedPanel.outerWidth(true);
		var boxWidth = this.fixedCart.outerWidth(true);
		var left = this.fixedCartOffset.left - (panelWidth - boxWidth);
		var top = this.isIE6 ? this.win.scrollTop() : 0;
			
		this.floatPanel.css({left: left, top: top});
		if(this.isIE6 && (top < this.fixedTop)) {
			this.floatPanel.addClass('my-cart-transparent').show();
			this.hasNotViewCss = true;
		}
		setFixed(this.settings.ids.floating);
	},
	_showFixedPanel: function(){
		this.fixedPanel.show();
		this.floatPanel.hide();
	},
	_showFloatingPanel: function() {
		if (this.hasNotViewCss) {
			this.floatPanel.removeClass('my-cart-transparent');
			this.hasNotViewCss = false;
		}
		this.fixedPanel.hide();
		this.floatPanel.show();
	},
	_addGoodsToCart: function(goodsId, quantity) {
		this._updateShippingCart();
	},
	_updateShippingCart: function() {
		var _this = this;
		this._updateCartPanelBody('<div class="item-loading">Loading...</div>');
		if(this.isClose) {
			this._showPanel();
		}
		
		$.ajax({
			type: 'POST',
			url: '/test/get-cart-items.php',
			data: 'uptime='+ this.updateTime,
			success: function(response){
				var result = $.parseJSON(response);
				var data = result.data;
				var html = '';
				var cart = $('<div></div>');
				
				html = compileTemplate(_this.template.cart, {
					itemcount: data.subtotal.itemcount,
					totalmoney: data.subtotal.money
				});
				cart.append(html);
				
				var ul = cart.find('ul'), li;
				html = '';
				for (var i = 0; i < data.items.length; i++) {
					html = compileTemplate(_this.template.goods, {
						album: data.items[i].album,
						url: data.items[i].url,
						title: data.items[i].title,
						qty: data.items[i].qty,
						price: data.items[i].price
					});
					
					li = $(html);
					if (i > 0) li.addClass('dashed');
					ul.append(li);
				}
				
				if (g_globals.objects.currency) {
					g_globals.objects.currency.converterToSettingCurrency(cart);
				}
				_this._updateCartPanelBody(cart.html());
				_this.updateTime = _this._getCartUpdateTime();
			}  
		});		
	},
	_isNeedUpdateCart: function() {
		var result = false;
		if ((0 == this.fixedPanel.find('.item-list').size()) || (this.updateTime != this._getCartUpdateTime())) {
			result = true;
		}
		return result;
	},
	_getCartUpdateTime: function() {
		return $.cookie('cart.uptime');
	},
	_updateCartPanelBody: function(html) {
		this.fixedPanel.html(html);
		this.floatPanel.html(html);
	},
	_updateCartItemCount: function() {
		var span = this.fixedCart.find('.item-count');
		if (1 == span.size()) {
			var c = $.cookie('cart.itemcount');
			c = Math.random();
			span.html(c);
		}
	}
};

(function($){  
	$.fn.actionSelect = function(options){ 
		return this.each(function(){
			new web.ActionSelect(options).bind($(this));
		});
	}
})(jQuery); 

